<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th><input v-model="isAll" type="checkbox" /> <span>全选</span></th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in goodList" :key="index">
          <th><input v-model="item.checked" type="checkbox" /></th>
          <th>{{ item.name }}</th>
          <th>{{ item.price }}</th>
          <th>
            <button @click="sub(index)">-</button> {{ item.num
            }}<button @click="add(index)">+</button>
          </th>
          <th>
            <span>{{ (sum = item.price * item.num) }}</span>
          </th>
          <th><button @click="del(index)">删除</button></th>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>合计:{{ sum2 }}</td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
export default {
  props: {
    goodList: {
      type: Array,
      required: true,
    },
  },
  methods: {
    //删除
    del(index) {
      this.$emit("del", index);
    },
    // 加数量
    add(index) {
      this.$emit("add", index);
    },
    //减数量
    sub(index) {
      this.$emit("sub", index);
    },
  },
  computed: {
    //全选
    isAll: {
      get() {
        return this.goodList.every((item) => item.checked);
      },
      set(val) {
        this.$emit("sett", val);
      },
    },
    sum2() {
      return this.goodList.reduce(
        (sum, item) => (sum = sum + item.num * item.price),
        0
      );
    },
  },
};
</script>

<style>
</style>